package com.esri.core.tasks.na;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.internal.util.d;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.Symbol;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class RouteResult extends NAResult {
    private List<Route> routes;
    private FeatureSet stops;

    private native void dispose(long j);

    private native SolveCondition[] getConditions(long j);

    private native RouteDirection[] getDirections(long j);

    private native byte[] getEnvelope(long j);

    private native long getLocalEndTime(long j);

    private native long getLocalStartTime(long j);

    private native byte[] getRoute(long j);

    private native double[] getTotalCosts(long j, String[] strArr);

    private native double getTotalViolation(long j);

    private native double getTotalWait(long j);

    RouteResult() {
        this.routes = new ArrayList();
        this.stops = null;
    }

    public List<Route> getRoutes() {
        return new ArrayList(this.routes);
    }

    public FeatureSet getStops() {
        if (this.stops == null) {
            return null;
        }
        FeatureSet featureSet = new FeatureSet();
        featureSet.setGraphics(this.stops.getGraphics());
        featureSet.setSpatialReference(this.stops.getSpatialReference());
        return featureSet;
    }

    public static RouteResult fromJson(JsonParser jsonParser) throws Exception {
        if (d.c(jsonParser)) {
            RouteResult routeResult = new RouteResult();
            HashMap hashMap = new HashMap();
            ArrayList arrayList = new ArrayList();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if ("directions".equals(currentName)) {
                    if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                            Route route = new Route();
                            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                                String currentName2 = jsonParser.getCurrentName();
                                jsonParser.nextToken();
                                if ("routeId".equals(currentName2)) {
                                    route.routeID = jsonParser.getIntValue();
                                } else if ("routeName".equals(currentName2)) {
                                    route.routeName = jsonParser.getText();
                                } else if ("summary".equals(currentName2)) {
                                    if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
                                        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                                            String currentName3 = jsonParser.getCurrentName();
                                            jsonParser.nextToken();
                                            if ("totalLength".equals(currentName3)) {
                                                route.totalLength = jsonParser.getDoubleValue();
                                            } else if ("totalTime".equals(currentName3)) {
                                                route.totalTime = jsonParser.getDoubleValue();
                                            } else if ("totalDriveTime".equals(currentName3)) {
                                                route.totalDriveTime = jsonParser.getDoubleValue();
                                            } else if ("envelope".equals(currentName3) && jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
                                                route.envelope = (Envelope) GeometryEngine.jsonToGeometry(jsonParser).getGeometry();
                                            }
                                        }
                                    }
                                } else if ("features".equals(currentName2) && jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                                        route.routingDirections.add(RouteDirection.fromJson(jsonParser));
                                    }
                                }
                            }
                            hashMap.put(Integer.valueOf(route.routeID), route);
                        }
                    }
                } else {
                    if ("routes".equals(currentName)) {
                        for (Graphic graphic : FeatureSet.fromJson(jsonParser, false, -1).getGraphics()) {
                            arrayList.add(graphic);
                        }
                    } else if ("stops".equals(currentName)) {
                        routeResult.stops = FeatureSet.fromJson(jsonParser, false, -1);
                    } else if ("barriers".equals(currentName)) {
                        routeResult.pointBarriers = FeatureSet.fromJson(jsonParser, false, -1);
                    } else if ("polylineBarriers".equals(currentName)) {
                        routeResult.polylineBarriers = FeatureSet.fromJson(jsonParser, false, -1);
                    } else if ("polygonBarriers".equals(currentName)) {
                        routeResult.polygonBarriers = FeatureSet.fromJson(jsonParser, false, -1);
                    } else if ("messages".equals(currentName)) {
                        if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                            routeResult.messages = new ArrayList();
                            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                                routeResult.messages.add(NAMessage.fromJson(jsonParser));
                            }
                        }
                    } else {
                        jsonParser.skipChildren();
                    }
                }
            }
            if (!hashMap.isEmpty()) {
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    Graphic graphic2 = (Graphic) it.next();
                    ((Route) hashMap.get(Integer.valueOf(((Integer) graphic2.getAttributeValue("ObjectID")).intValue()))).route = graphic2;
                }
                routeResult.routes.addAll(hashMap.values());
            } else {
                Iterator it2 = arrayList.iterator();
                while (it2.hasNext()) {
                    Route route2 = new Route();
                    route2.route = (Graphic) it2.next();
                    routeResult.routes.add(route2);
                }
            }
            return routeResult;
        }
        return null;
    }

    @Override // com.esri.core.tasks.na.NAResult
    public int hashCode() {
        List<Route> list = this.routes;
        int hashCode = ((list == null ? 0 : list.hashCode()) + 31) * 31;
        FeatureSet featureSet = this.stops;
        return hashCode + (featureSet != null ? featureSet.hashCode() : 0);
    }

    @Override // com.esri.core.tasks.na.NAResult
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && getClass() == obj.getClass()) {
            RouteResult routeResult = (RouteResult) obj;
            List<Route> list = this.routes;
            if (list == null) {
                if (routeResult.routes != null) {
                    return false;
                }
            } else if (!list.equals(routeResult.routes)) {
                return false;
            }
            FeatureSet featureSet = this.stops;
            if (featureSet == null) {
                if (routeResult.stops != null) {
                    return false;
                }
            } else if (!featureSet.equals(routeResult.stops)) {
                return false;
            }
            return true;
        }
        return false;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("RouteResult [routes=");
        List<Route> list = this.routes;
        sb.append(list != null ? list.subList(0, Math.min(list.size(), 3)) : null);
        sb.append(", stops=");
        sb.append(this.stops);
        sb.append("]");
        return sb.toString();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public RouteResult(long j, RouteParameters routeParameters) {
        String[] strArr;
        String[] messagesForCondition;
        this.routes = new ArrayList();
        this.stops = null;
        Route route = new Route();
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        Geometry geometryFromEsriShape = GeometryEngine.geometryFromEsriShape(getEnvelope(j), Geometry.Type.ENVELOPE);
        Geometry geometryFromEsriShape2 = GeometryEngine.geometryFromEsriShape(getRoute(j), Geometry.Type.POLYLINE);
        RouteDirection[] directions = getDirections(j);
        SolveCondition[] conditions = getConditions(j);
        if (conditions != null && conditions.length > 0) {
            this.messages = new ArrayList();
            int length = conditions.length;
            int i = 0;
            while (i < length) {
                SolveCondition solveCondition = conditions[i];
                if (solveCondition != null && (messagesForCondition = solveCondition.getMessagesForCondition(routeParameters)) != null) {
                    int length2 = messagesForCondition.length;
                    int i2 = 0;
                    while (i2 < length2) {
                        this.messages.add(new NAMessage(50, messagesForCondition[i2]));
                        i2++;
                        conditions = conditions;
                    }
                }
                i++;
                conditions = conditions;
            }
        }
        ArrayList arrayList = new ArrayList(Arrays.asList(routeParameters.getAccumulateAttributeNames()));
        arrayList.add(routeParameters.getImpedanceAttributeName());
        double[] totalCosts = getTotalCosts(j, (String[]) arrayList.toArray(new String[arrayList.size()]));
        for (int i3 = 0; i3 < totalCosts.length; i3++) {
            linkedHashMap.put("Total_" + strArr[i3], Double.valueOf(totalCosts[i3]));
        }
        double totalViolation = getTotalViolation(j);
        double totalWait = getTotalWait(j);
        linkedHashMap.put("TotalViolation_" + routeParameters.getImpedanceAttributeName(), Double.valueOf(totalViolation));
        linkedHashMap.put("TotalWait_" + routeParameters.getImpedanceAttributeName(), Double.valueOf(totalWait));
        route.totalWaitTime = totalWait;
        route.totalViolationTime = totalViolation;
        linkedHashMap.put("StartTime", Long.valueOf(getLocalStartTime(j)));
        linkedHashMap.put("EndTime", Long.valueOf(getLocalEndTime(j)));
        double d = 0.0d;
        double d2 = 0.0d;
        for (RouteDirection routeDirection : directions) {
            d += routeDirection.getMinutes();
            d2 += routeDirection.getLength();
        }
        route.totalLength = d2;
        route.totalDriveTime = d;
        route.envelope = (Envelope) geometryFromEsriShape;
        route.totalTime = route.totalDriveTime + route.totalStopTime + route.totalWaitTime;
        route.route = new Graphic(geometryFromEsriShape2, (Symbol) null, linkedHashMap);
        route.routingDirections = Arrays.asList(directions);
        if (routeParameters.isReturnStops()) {
            FeatureSet featureSet = new FeatureSet();
            Graphic[] cloneStops = ((LocalRouteParameters) routeParameters).cloneStops();
            if (cloneStops != null) {
                featureSet.setGraphics(cloneStops);
                featureSet.setSpatialReference(routeParameters.getOutSpatialReference());
                this.stops = featureSet;
            }
        }
        if (routeParameters.isReturnPointBarriers()) {
            FeatureSet featureSet2 = new FeatureSet();
            Graphic[] clonePointBarriers = ((LocalRouteParameters) routeParameters).clonePointBarriers();
            if (clonePointBarriers != null) {
                featureSet2.setGraphics(clonePointBarriers);
                featureSet2.setSpatialReference(routeParameters.getOutSpatialReference());
                this.pointBarriers = featureSet2;
            }
        }
        if (routeParameters.isReturnPolylineBarriers()) {
            FeatureSet featureSet3 = new FeatureSet();
            Graphic[] clonePolylineBarriers = ((LocalRouteParameters) routeParameters).clonePolylineBarriers();
            if (clonePolylineBarriers != null) {
                featureSet3.setGraphics(clonePolylineBarriers);
                featureSet3.setSpatialReference(routeParameters.getOutSpatialReference());
                this.polylineBarriers = featureSet3;
            }
        }
        if (routeParameters.isReturnPolygonBarriers()) {
            FeatureSet featureSet4 = new FeatureSet();
            Graphic[] clonePolygonBarriers = ((LocalRouteParameters) routeParameters).clonePolygonBarriers();
            if (clonePolygonBarriers != null) {
                featureSet4.setGraphics(clonePolygonBarriers);
                featureSet4.setSpatialReference(routeParameters.getOutSpatialReference());
                this.polygonBarriers = featureSet4;
            }
        }
        this.routes.add(route);
        dispose(j);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void updateConvenience(String str, String str2, double d, double d2, double d3) {
        for (Route route : this.routes) {
            route.updateConvenience(str, str2, d, d2, d3);
        }
    }
}
